﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace CXMLStudioIDE.Common.Entities
{
    public class EntityInstanceCollection : CollectionBase
    {
        #region Methods

        public EntityInstance this[int index]
        {
            get
            {
                return ((EntityInstance)List[index]);
            }
            set
            {
                List[index] = value;
            }
        }

        public int Add(EntityInstance value)
        {
            return (List.Add(value));
        }

        public int IndexOf(Project value)
        {
            return (List.IndexOf(value));
        }

        public void Insert(int index, EntityInstance value)
        {
            List.Insert(index, value);
        }

        public void Remove(EntityInstance value)
        {
            List.Remove(value);
        }

        public ArrayList GetList()
        {
            return InnerList;
        }

        public bool hasElements()
        {
            return (InnerList.Count > 0 ? true : false);
        }

        public EntityInstance GetEntityInstanceByTreeviewID(int i_TreeViewID)
        {
            foreach (EntityInstance e in this)
            {
                if (e.TreeViewID == i_TreeViewID)
                    return e;
            }
            return null;
        }

        //Creates a DataTable from the projects array.
        //The project business entity are added as project children
        //public DataTable GetProjectsDataTable(Solution m_solution)
        //{
        //    bool hasSolution = (m_solution == null ? false : true);

        //    DataTable table = new DataTable();
        //    table.Columns.Add("ID", typeof(int));
        //    table.Columns.Add("title", typeof(string));
        //    table.Columns.Add("parentID", typeof(string));
        //    int id = (hasSolution ? 2 : 1);
        //    int parent_id = (hasSolution ? 1 : 0);
        //    int business_entity_id = 0;
        //    int project_id = 0;

        //    if (hasSolution)
        //    {
        //        table.Rows.Add(1, "Solution '" + m_solution.Name + "'", 0);
        //    }

        //    this.Sort();

        //    //Here we add the rows. If there is a solution then we add a solution root element
        //    for (int i = 0; i < this.Count; i++)
        //    {
        //        //TODO: Add logic to handle multiple business entities
        //        Project p = (Project)this[i];
        //        //table.Rows.Add(id, "Project '" + this.Name + "' Scope:[" + this.Scope.Name + "] Context:[" + this.Context.Name + "]", parent_id);
        //        table.Rows.Add(id, "Project '" + p.Name + "' Scope:[" + p.Scope.Name + "] Context:[" + p.Context.Name + "]", parent_id);
        //        p.TreeViewID = id;
        //        project_id = id;
        //        table.Rows.Add(++id, "Business Entities", id - 1);
        //        business_entity_id = id;

        //        this[i].Entities.Sort();

        //        for (int j = 0; j < this[i].Entities.Count; j++)
        //        {
        //            table.Rows.Add(++id, this[i].Entities[j].Name + ". Version:[" + this[i].Entities[j].EntityVersion.FullVersion + "]", business_entity_id);
        //            this[i].Entities[j].TreeViewID = id;
        //        }
        //        ++id;
        //        ++project_id;
        //    }
        //    return table;
        //}

        public EntityInstance GetEntityInstanceByID(int ID)
        {
            foreach (EntityInstance e in this)
            {
                if (e.ID == ID)
                {
                    return e;
                }
            }
            return null;
        }

        //public static Project GetProjectContainingBusinessEntityByTreeViewID(ProjectCollection pec, int _BETreeviewID)
        //{
        //    foreach (Project p in pec)
        //    {
        //        foreach (BusinessEntity b in p.Entities)
        //        {
        //            if (b.TreeViewID == _BETreeviewID)
        //                return p;
        //        }
        //    }
        //    return null;
        //}

        public void Sort()
        {
            IComparer NameSorter = new SortProjectByNameClass();
            InnerList.Sort(NameSorter);
        }

        #endregion

    }
    public class SortEntityInstanceByDescClass : IComparer
    {
        public int Compare(object obj1, object obj2)
        {
            EntityInstance e1 = (EntityInstance)obj1;
            EntityInstance e2 = (EntityInstance)obj2;

            return (String.Compare(e1.Description, e2.Description));
        }

    }
}
